/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Deal.ViewController;

import Deal.PersonalInformation.Logic.BasicInformation;
import Deal.PersonalInformation.Logic.CollectionLogic;
import Deal.PersonalInformation.Logic.CouponLogic;
import Deal.PersonalInformation.Logic.DiscountCouponLogic;
import Deal.PersonalInformation.Logic.EquivalentCouponLogic;
import Deal.PersonalInformation.Logic.OrderLogic;
import Deal.SearchBook.Logic.SearchLogic;
import Deal.ShoppingCart.Logic.SettleAccountLogic;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 *
 * @author 伯乐
 */
public class ViewController {
    private SearchLogic searchLogic = new SearchLogic();
    private BasicInformation basicInformation = new BasicInformation();
    private CollectionLogic collection = new CollectionLogic();
    private OrderLogic orderLogic = new OrderLogic();
    private CouponLogic couponLogic;
    private SettleAccountLogic settleAccountLogic = new SettleAccountLogic();
    private String user_id;
    
    /**
     *
     * @param USERID
     */
    public ViewController(String USERID){
        user_id = USERID;
    }

    public boolean addToCollection(String key, Object[] book) {
        String list[] = new String[book.length];
        for(int i=0; i<book.length; i++){
            list[i] = book[i].toString();
        }
        return collection.addToCollection(user_id, list);
    }

    public boolean removeFromCollection(String collectionID) {
        return collection.removeCollection(user_id, collectionID);
    }

    public Vector<List> collectionTableVector() {
        return collection.CollectionTableVector(user_id);
    }

    public String getUserName() {
        return basicInformation.getUserName(user_id);
    }
    
    public String getUserID(){
        return user_id;
    }

    public int getUserPoints() {
        return basicInformation.getUserPoints(user_id);
    }

    public boolean setUserCode(String text) {
        return basicInformation.setCode(user_id, text);
    }

    public Vector orderTableVector(String id) {
        return orderLogic.orderTableVector(user_id);
    }
   
    public Object[] getBookCategory() {
        Object[] str = searchLogic.getBookCategory().toArray();
        Object[] bookCatalog = new String[str.length+1];
        bookCatalog[0] = "所有类别";
        System.arraycopy(str, 0, bookCatalog, 1, str.length);
        return bookCatalog;
    }

    public Vector<List> searchCategory(String category) {
        return searchLogic.searchCategoryTableVector(category);
    }

    public Vector<List> searchKeywords(String key, String keywords) {
        return searchLogic.searchKeywordsTableVector(key, keywords);
    }
    
    public Vector<List> searchAndTable(String category, String key, String keywords){//同时符合category和keywords
        return searchLogic.searchAnd(category, key, keywords);
    }
    
    public Vector<List> searchOrTable(String keywords){
        return searchLogic.searchOr(keywords);
    }
    
    public Vector<List> getDisCoupon(){
        couponLogic = new DiscountCouponLogic();
        return couponLogic.getCoupon(user_id);
    }
    
    public Vector<List> getEqCoupon(){
        couponLogic = new EquivalentCouponLogic();
        return couponLogic.getCoupon(user_id);
    }
    
    public Vector<List> getCoupon(){
        Vector<List> list = getDisCoupon();
        Vector<List> list2 = getEqCoupon();
        if(list2!=null){
            list.addAll(list2);
        }
        return list;
    }

    public boolean dealDone(String type, int point, double account,
            List<String> couponList, List<Integer> numList, List<Object[]> books,
            String name, String phone, String postcode, String address) {
        List bookList = new ArrayList();
        for(Object[] objectArr : books){  //格式转换
            String[] arr = new String[objectArr.length];
            for(int i=0; i<objectArr.length; i++){
                arr[i] = String.valueOf(objectArr[i]);
            }
            bookList.add(arr);
        }
        return settleAccountLogic.dealDone(type, user_id, point, account, couponList, numList, bookList, name, phone, postcode, address);
    }

}
